package com.music.web.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.music.biz.AlbumBiz;
import com.music.biz.GenreBiz;
import com.music.biz.OrderBiz;
import com.music.biz.OrderDetailsBiz;
import com.music.biz.UserBiz;
import com.music.entity.Album;
import com.music.entity.Genre;
import com.music.entity.Order;
import com.music.entity.OrderDetail;
import com.music.entity.ShoppingCart;
import com.music.entity.User;

@Controller
@RequestMapping("/hom")
public class Background {

	@Autowired
	private UserBiz userBiz;

	@Autowired
	private AlbumBiz albumBiz;

	@Autowired
	private OrderBiz orderBiz;

	@Autowired
	private GenreBiz genreBiz;
	
	
	@Autowired
	private OrderDetailsBiz orderDetailsBiz;

	// 后台主页进入
	@RequestMapping("/homepage")
	public String homepage() {
		return "background/admin_default";
	}

	// 商品管理
	@RequestMapping("/album_management")
	public String merchandise(Model model) {
		List<Album> albums = albumBiz.getHotAlbums(0);
		model.addAttribute("albums", albums);
		return "background/album_management";
	}

	// 进入商品添加
	@RequestMapping("/album_edit")
	public String album_edit(Model model) {
		model.addAttribute("message", "添加商品");
		List<Genre> genres = genreBiz.getAll();
		model.addAttribute("genres", genres);
		model.addAttribute("mo", "album_add");
		return "background/album_edit";
	}

	// 进入商品修改
	@RequestMapping("/album_update")
	public String album_update(int id, Model model) {
		model.addAttribute("message", "修改商品");
		Album album = albumBiz.fetchById(id);
		List<Genre> genres = genreBiz.getAll();
		model.addAttribute("genres", genres);
		model.addAttribute("album", album);
		return "background/album_edit";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String album_add(
			Album album,
			Model model,
			@RequestParam(name = "albumImage", required = false) MultipartFile albumImage,
			HttpServletRequest request) {

		if (album.getId() != 0) {
			albumBiz.update(album);
		} else {
			albumBiz.add(album);
		}
		if (!albumImage.isEmpty()) {
			String path = request.getSession().getServletContext()
					.getRealPath("CoverImages");
			String fileName = path + "\\" + album.getId() + ".jpg";
			System.out.println(fileName);
			try {
				FileOutputStream fos = new FileOutputStream(fileName);
				fos.write(albumImage.getBytes());
				fos.flush();
				fos.close();
				System.out.println("上传成功。。。");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return "redirect:album_management";
	}

	// 用户管理
	@RequestMapping("/user_management")
	public String user_management(Model model) {
		List<User> users = userBiz.getAll();
		model.addAttribute("users", users);
		return "background/user_management";
	}

	// 用户删除
	@RequestMapping("/delete_user")
	public String delete_user(int id, Model model) {
		userBiz.delete(id);
		List<User> users = userBiz.getAll();
		model.addAttribute("users", users);
		return "redirect:user_management";
	}

	// 订单管理
	@RequestMapping("/orde_management")
	public String orde_management(Model model) {
		List<Order> orders = orderBiz.getAll();
		model.addAttribute("orders", orders);
		return "background/orde_management";
	}

	// 订单查询
	@RequestMapping(value=("/selectOrde"),method=RequestMethod.POST)
	public String selectOrde(
			@RequestParam(defaultValue = "0", required = false) int state,
			@RequestParam(defaultValue = "0", required = false) int id,
			String startTime, String endTime, Model model) {
		List<Order> orders = orderBiz.getOrders(state, id, endTime, startTime);
		model.addAttribute("orders", orders);
		return "background/orde_management";
	}

	//订单详情
		@RequestMapping("/orde_details")
		public String orde_details(int id,Model model) {
			Order order =orderBiz.getOrderById(id);
			List<OrderDetail> orderDetails =orderDetailsBiz.getoDetailsById(id);
			model.addAttribute("order", order);
			model.addAttribute("orderDetails", orderDetails);
			return "background/orde_details";
		}
	
	// 订单状态修改
	@RequestMapping("/statusUpdate")
	public String statusUpdate(int id) {
		orderBiz.updateState(id);
		return "redirect:orde_management";
	}
	
	//分类进入
	@RequestMapping("/classify")
	public String classify(Model model) {
		List<Genre> genres =genreBiz.getAll();
		model.addAttribute("genres", genres);
		return "background/classify_management";
	}
	
	@RequestMapping("/sleectClassify")
	@ResponseBody
	public String sleectClassify(String name) {
		List<Genre> genres =genreBiz.getGenresByName(name);
		return  JSONArray.toJSONString(genres);
	}
	
	@RequestMapping("/getClassify")
	@ResponseBody
	public String getClassify() {
		List<Genre> genres =genreBiz.getAll();
		return  JSONArray.toJSONString(genres);
	}
	
	@RequestMapping("/updateClassifyById")
	@ResponseBody
	public void updateClassifyById(@RequestParam(defaultValue = "0", required = false)int id,String name,String description) {
		Genre genre =new Genre(id, name, description);
		if(id>0){
			genreBiz.update(genre);
		}else {
			genreBiz.add(genre);
		}
		
		
	}
	
	@RequestMapping("/deleteClassifyById")
	@ResponseBody
	public void deleteClassifyById(@RequestParam(defaultValue = "0", required = false)int id) {
		genreBiz.delete(id);
	}
	
	@RequestMapping("/sleectClassifyById")
	@ResponseBody
	public String sleectClassifyById(int id) {
		Genre genre =genreBiz.fetchById(id);
		return  JSONArray.toJSONString(genre);
	}
	
}
